package com.database.utils;

import com.database.common.Service;

import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA by ChouFy on 2020-02-28.
 *
 * @author zhoufy
 */
@Service
public class TransactionManager {


    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }


    public void beginTransaction() throws SQLException {
        System.out.println("beginTransaction()");
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    }

    /**
     * 提交事务
     *
     * @throws SQLException
     */
    public void commit() throws SQLException {
        System.out.println("commit()");

        connectionUtils.getCurrentThreadConn().commit();
    }


    /**
     * 回滚事务
     *
     * @throws SQLException
     */
    public void rollback() throws SQLException {
        System.out.println("rollback()");
        connectionUtils.getCurrentThreadConn().rollback();
    }

}
